package com.ccy.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccy.common.utils.PageUtils;
import com.ccy.common.utils.Query;
import com.ccy.ware.dao.WareInfoDao;
import com.ccy.ware.entity.WareInfoEntity;
import com.ccy.ware.service.WareInfoService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");
        String sidx = (String) params.get("sidx");
        String order = (String) params.get("order");
        String orderRules = String.format("order by %s %s", sidx, order);
        LambdaQueryWrapper<WareInfoEntity> queryWrapper = Wrappers.<WareInfoEntity>lambdaQuery()
                .like(StringUtils.isNotBlank(key), WareInfoEntity::getId, key)
                .or()
                .like(StringUtils.isNotBlank(key), WareInfoEntity::getName, key)
                .last(StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order), orderRules);
        IPage<WareInfoEntity> page = this.page(new Query<WareInfoEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }
}